package com.hpu.parser;

import com.hpu.bean.Book;
import com.hpu.bean.Electric;
import com.hpu.bean.ElectricSummary;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ElectricParser {

	public ElectricSummary parse(String parseString) {
		ElectricSummary summary=new ElectricSummary();
		List<Electric> electrics = new ArrayList<>();
		try {
			JSONObject object=new JSONObject(parseString);
			summary.setSurplus(object.getString("rest"));
			JSONArray array = object.getJSONArray("detail");
			for(int i=0;i<array.length();i++){
				JSONArray array2=array.getJSONArray(i);
				String time=array2.getString(0);
				String type=array2.getString(1);
				String money=array2.getString(2);
				Electric electric=new Electric(time,type,money);
				electrics.add(electric);
			}
			summary.setElectricList(electrics);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return summary;
	}
}
